﻿using System;
using System.Collections.Generic;
using System.Data;    
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.DBM;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class MoneyRequestCollateral : System.Web.UI.UserControl
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        public void LoadMoneyRequestCollateral(string strMoneyReqSeq)
        {
            M_FIM_MoneyRequests info = mr.GetMoneyReqDNInfo(strMoneyReqSeq);

            dpMoneyReqDate.SetValue(info.money_req_date);
            lblMoneyReqSeq.Text = info.money_req_seq.ToString();
            lblWorkAreaSeq.Text = info.work_area_seq.ToString();
            lblRefNo.Text = info.ref_no;
            lblIcmRefNo.Text = info.dn_ref_no;
            lblBgmYear.Text = info.bgm_year.ToString();
            lblBgmSourceSeq.Text = info.bgm_source_seq.ToString();
            lblIcmDnSeq.Text = info.icm_dn_seq.ToString();

            lblNetAmount.Text = info.total_amount.Value.ToString("#,##0.00");
            lblPercentVat.Text = info.percent_vat.Value.ToString("#,##0.00");
            lblPercentTax.Text = info.percent_tax.Value.ToString("#,##0.00");
            lblVatAmount.Text = info.total_amount_vat.Value.ToString("#,##0.00");
            lblTaxAmount.Text = info.total_amount_tax.Value.ToString("#,##0.00");
            lblPenaltyAmount.Text = info.total_amount_penalty.Value.ToString("#,##0.00");
            lblRedemptionAmount.Text = info.total_amount_redemption.Value.ToString("#,##0.00");
            lblDeductionAmount.Text = info.total_amount_deduction.Value.ToString("#,##0.00");
            lblAmountNoVat.Text = (info.total_amount - info.total_amount_vat).Value.ToString("#,##0.00");

            LoadDataRadGrid1();
        }

        protected void LoadDataRadGrid1()
        {
            DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
            string strIcmDnSeq = lblIcmDnSeq.Text;
            string strSQL = "SELECT dn.ICM_DN_ITEM_SEQ"
                            + ", v.ICM_VAT_SEQ"
                            + ", CAST(CONCAT(v.ICM_VAT_NAME, ' อัตราภาษี ', b.VAT_PERCENT , ' %') as varchar) AS ICM_VAT_NAME"
                            + ", b.VAT_PERCENT"
                            + ", COALESCE(i.ICM_RES_ITEM_NAME, x.BGM_EXP_NAME) AS ITEM_NAME"
                            + ", dn.ITEM_DESCR"
                            + ", dn.ITEM_QTY"
                            + ", u.ICM_UNIT_NAME"
                            + ", 1 AS PO_ITEM_QTY"
                            //+ ", CAST(CONCAT(COALESCE(dn.ITEM_QTY, '0'), ' ', COALESCE(u.ICM_UNIT_NAME, 'รายการ')) as varchar) AS PO_ITEM_QTY"
                            + ", pi.NET_AMOUNT AS PO_NET_AMOUNT"
                            + ", dn.ITEM_PRICE_EXCLUDE_VAT"
                            + ", dn.ITEM_PRICE_INCLUDE_VAT"
                            + ", dn.EXCLUDE_VAT_AMOUNT"
                            + ", dn.VAT_AMOUNT"
                            + ", dn.NET_AMOUNT"
                        + " FROM ICM_DELIVERY_NOTE_ITEM dn"
                            + " INNER JOIN ICM_PURCHASE_ORDER_INSTALLMENT_ITEM pi ON dn.ICM_PO_INSTALL_ITEM_SEQ = pi.ICM_PO_INSTALL_ITEM_SEQ"
                            + " INNER JOIN ICM_BIDDER_QUOTATION q ON pi.ICM_BIDDER_QUO_SEQ = q.ICM_BIDDER_QUO_SEQ"
                            + " INNER JOIN ICM_BIDDER b ON q.ICM_BIDDER_SEQ = b.ICM_BIDDER_SEQ "
                            + " INNER JOIN ICM_PURCHASE_REQUEST_ITEM ri ON q.ICM_PR_ITEM_SEQ = ri.ICM_PR_ITEM_SEQ "
                            + " INNER JOIN BGM_MAS_EXPENSE x ON ri.BGM_EXP_SEQ = x.BGM_EXP_SEQ "
                            + " LEFT JOIN ICM_MAS_RESOURCE_ITEM i ON ri.ICM_RES_ITEM_SEQ = i.ICM_RES_ITEM_SEQ "
                            + " LEFT JOIN ICM_MAS_UNIT u ON i.ICM_UNIT_SEQ = u.ICM_UNIT_SEQ "
                            + " INNER JOIN ICM_SYS_VAT v ON b.ICM_VAT_SEQ = v.ICM_VAT_SEQ "
                        + " WHERE dn.ICM_DN_SEQ = " + strIcmDnSeq;
            dbManager.Open();
            DataTable dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            dbManager.Dispose();

            RadGrid1.DataSource = dtb;
            RadGrid1.DataBind();

            double dSumAmount = 0;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                dSumAmount += Convert.ToDouble(RadGrid1.Items[i]["NET_AMOUNT"].Text);
            }
            lblSumAmount.Text = dSumAmount.ToString();
        }

        protected void ClearControl1()
        {
        }

        //=====================================================================

        public bool Insert()
        {
            bool bOK = true;
            return bOK;
        }

        public bool Update()
        {
            bool bOK = true;
            M_FIM_MoneyRequests info = new M_FIM_MoneyRequests();
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strBgmYear = lblBgmYear.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strEventSeq = "14";
            string strUseAdvance = "0";
            string strMoneyReqDate = dpMoneyReqDate.GetStringValue();
            string strMoneyReqName = "";
            string strRefNo = lblRefNo.Text;

            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;

            info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.use_advance = strUseAdvance;
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.money_req_name = strMoneyReqName;
            info.ref_no = strRefNo;
            info.approver_seq = null;
            info.emp_seq = main.ConvertNullInt(strUserID);
            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            mr.Update(info);

            M_FIM_MoneyRequestDtls dtlsInfo = new M_FIM_MoneyRequestDtls();
            M_FIM_SystemControls sys = main.GetSystemControls();
            string strMoneyReqDtlsSeq = mrd.GetList(strMoneyReqSeq)[0].money_req_dtls_seq.ToString();

            dtlsInfo.money_req_dtls_seq = Convert.ToInt32(strMoneyReqDtlsSeq);
            dtlsInfo.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
            dtlsInfo.money_req_dtls_name = null;
            dtlsInfo.bgm_proj_oper_exp_seq = null;//main.ConvertNullInt(DropDownList_BgmProjectExpense1.GetSelectedValue());
            dtlsInfo.percent_vat = 0;
            dtlsInfo.percent_tax = 0;
            dtlsInfo.amount = main.ConvertNullDouble(lblSumAmount.Text);
            dtlsInfo.amount_vat = 0;
            dtlsInfo.amount_tax = 0;
            dtlsInfo.amount_fund = 0;
            //dtlsInfo.currency_seq = sys.default_currency_seq;
            //dtlsInfo.exchange_rate = 1;
            //dtlsInfo.bath_amount = main.ConvertNullDouble(lblSumAmount.Text);
            //dtlsInfo.bath_amount_vat = 0;
            //dtlsInfo.bath_amount_tax = 0;
            //dtlsInfo.bath_amount_fund = 0;
            dtlsInfo.updated_by = main.ConvertNullInt(strUserID);
            dtlsInfo.updated_when = main.ConvertNullDateTime(dtNow);

            mrd.Update(dtlsInfo);
            ClearControl1();

            return bOK;
        }
    }
}